<!--头部公共部分-->
{include file="common/header" /}

<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <input type="text" style="width: 200px;" name="param" placeholder="请输入学生姓名/学号/电话" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>

            </div>
        </div>

    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
    <div class="layui-form-item"  style="text-align: right">

        <a class="layui-btn layui-btn-normal" lay-submit lay-filter="sure">确定</a>
        <a class="layui-btn  layui-btn-primary" lay-submit lay-filter="cancel">取消</a>

    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table','form', 'laydate','element',  'layer'], function () {
        let table = layui.table,
            laydate = layui.laydate,
            form = layui.form,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        let loading = layer.load();
        var info_id = '{$info_id}';
        var campus_id = '{$campus_id}';
        var status = '{$status}';
        //方法级渲染
        let tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("arranging/add_students")}?campus_id='+campus_id
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'student_number', title: '学号',width: 230, style:'color:#01AAED;font-weight: bold;', align: 'center', templet: '#name'},
                {field: 'name', title: '姓名', width: 150,align: 'center'},
                {field: 'student_type', title: '类型',style:'color:#FF4500;font-weight: bold;', width: 120,align: 'center'},
                {field: 'intentional_course', title: '意向课程', width: 300,align: 'center'},
                {field: 'sex', title: '性别', width: 100,align: 'center'},
                {field: 'phone', title: '手机号', width: 150,align: 'center'},
                {field: 'grade', title: '年级', width: 120,align: 'center'},
                {field: 'campus_name', title : '<span title="学员首次登记的校区">报名校区<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span>',width: 150,align: 'center'},

            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });


        //搜索项
        $('#search').click(function () {
            let field = {};
            field.param = $('input[name=param]').val();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'param': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });


        //监听取消
        form.on('submit(cancel)', function (data) {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

        //监听确定
        form.on('submit(sure)', function (data) {
            var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            if (checkStatus.data.length == 0) {
                layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
                });
                return false;
            }

            var ids = '';
            for(var i =0; i < checkStatus.data.length; i ++){
                ids = ids + checkStatus.data[i]['id'] + ',';
            }
            ids = ids.substring(0,ids.length-1)
            var field = {};
            field.ids = ids;
            field.info_id = info_id;
            field.status = status;
            $.ajax({
                url: '/admin/arranging/submit_students',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        location.href = "{:url('Arranging/roll_call')}?id="+info_id + '&campus_id='+ campus_id
                    }

                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;


        });

    });
</script>

